{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# Machine Learning Matching(机器学习匹配)"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import modules 导入模块\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import csv\n",
    "import py_entitymatching as em\n",
    "import os"
   ]
  },
  {
   "source": [
    "## 读取样本文件"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "filepath1 = \"datasets/DBLP-Scholar/DBLP1.csv\"\n",
    "filepath2 = \"datasets/DBLP-Scholar/Scholar.csv\"\n",
    "filepath3 = \"datasets/DBLP-Scholar/DBLP-Scholar_perfectMapping.csv\"\n",
    "\n",
    "dblp = pd.read_csv(filepath1, encoding=\"iso8859-1\")\n",
    "scholar = pd.read_csv(filepath2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "Metadata file is not present in the given path; proceeding to read the csv file.\n"
     ]
    }
   ],
   "source": [
    "S = em.read_csv_metadata(\"datasets/DBLP-Scholar/candidates_sample.csv\", key='_id',\n",
    "                         ltable=dblp, rtable=scholar,\n",
    "                         fk_ltable='ltable_id', fk_rtable='rtable_id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "   Unnamed: 0  _id                  ltable_id  \\\n",
       "0           9    9           conf/sigmod/2000   \n",
       "1          53   53           conf/sigmod/2002   \n",
       "2          61   61           conf/sigmod/2003   \n",
       "3          77   77           conf/sigmod/2003   \n",
       "4         102  102  conf/sigmod/AbiteboulCM95   \n",
       "5         115  115   conf/sigmod/AcharyaAFZ95   \n",
       "6         155  155      conf/sigmod/AdaliSS99   \n",
       "7         161  161     conf/sigmod/Adelberg98   \n",
       "8         167  167   conf/sigmod/AdelbergGK95   \n",
       "9         208  208   conf/sigmod/AgichteinG03   \n",
       "\n",
       "                                                                                             rtable_id  \\\n",
       "0                                                                                         SHYtwo2v-LAJ   \n",
       "1                                                 url:http://portal.acm.org/citation.cfm%3Fid%3D582192   \n",
       "2                                                                                         CpwYam_LYyEJ   \n",
       "3  url:http://portal.acm.org/citation.cfm%3Fid%3D276304%26dl%3DACM%26coll%3Dportal%26CFID%3D1111111...   \n",
       "4                                                                                         XTa-ElbpjZ8J   \n",
       "5                                                                                         h2z51QPXcSkJ   \n",
       "6                                                                                         Cbwq3JSxrSAJ   \n",
       "7                                                                                         JrU1gnfH1sYJ   \n",
       "8                                                                                         b87rz7o7R94J   \n",
       "9                                                                                         1HwyWUB6M50J   \n",
       "\n",
       "                                                                                          ltable_title  \\\n",
       "0  proceedings of the 2000 acm sigmod international conference on management of data, may 16-18, 20...   \n",
       "1  proceedings of the 2002 acm sigmod international conference on management of data, madison, wisc...   \n",
       "2  proceedings of the 2003 acm sigmod international conference on management of data, san diego, ca...   \n",
       "3  proceedings of the 2003 acm sigmod international conference on management of data, san diego, ca...   \n",
       "4                                                                a database interface for file updates   \n",
       "5                          broadcast disks: data management for asymmetric communications environments   \n",
       "6                                                                    a multimedia presentation algebra   \n",
       "7           nodose - a tool for semi-automatically extracting semi-structured data from text documents   \n",
       "8                                          applying update streams in a soft real-time database system   \n",
       "9             qxtract: a building block for efficient information extraction from plain-text databases   \n",
       "\n",
       "                              ltable_authors  ltable_year  \\\n",
       "0                                        NaN         2000   \n",
       "1                                        NaN         2002   \n",
       "2                                        NaN         2003   \n",
       "3                                        NaN         2003   \n",
       "4               s abiteboul, s cluet, t milo         1995   \n",
       "5  s acharya, r alonso, m franklin, s zdonik         1995   \n",
       "6          s adali, m sapino, v subrahmanian         1999   \n",
       "7                                 b adelberg         1998   \n",
       "8         b adelberg, h garcia-molina, b kao         1995   \n",
       "9                     e agichtein, l gravano         2003   \n",
       "\n",
       "                                                                                          rtable_title  \\\n",
       "0  w3qs: a query system for the world-wide webâ» in proceedings of the 21st international conferenc...   \n",
       "1                                        source international conference on management of data archive   \n",
       "2  on predicting data cache behavior for real {time systems. in proceedings of the acm sigplan work...   \n",
       "3                                        source international conference on management of data archive   \n",
       "4                                                                     a logical view of structured les   \n",
       "5                         broadca. rt l) ikr: data management for uymmetrir communications environment   \n",
       "6                                                                    a multimedia presentation algebra   \n",
       "7                      nodose {a tool for semi-automatically extracting semi-structured data from text   \n",
       "8                                                                                       rules in strip   \n",
       "9                                         querying text databases for efficient information extraction   \n",
       "\n",
       "                               rtable_authors  rtable_year  label  \n",
       "0                     d konopnicki, o shmueli            0      0  \n",
       "1                               m stonebraker         1992      0  \n",
       "2                      c ferdinand, r wilhelm            0      0  \n",
       "3                        a tiwary, m franklin         1998      0  \n",
       "4                s abiteboul, s cluet, t milo            0      0  \n",
       "5  s acharya, r alonso, mj franklin, s zdonik         1995      1  \n",
       "6          s adah, ml sapino, vs subrahmanian         1999      1  \n",
       "7                                  b adelberg            0      1  \n",
       "8          b adelberg, h garcia-molina, b kao            0      0  \n",
       "9                      e agichtein, l gravano            0      0  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Unnamed: 0</th>\n      <th>_id</th>\n      <th>ltable_id</th>\n      <th>rtable_id</th>\n      <th>ltable_title</th>\n      <th>ltable_authors</th>\n      <th>ltable_year</th>\n      <th>rtable_title</th>\n      <th>rtable_authors</th>\n      <th>rtable_year</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>9</td>\n      <td>9</td>\n      <td>conf/sigmod/2000</td>\n      <td>SHYtwo2v-LAJ</td>\n      <td>proceedings of the 2000 acm sigmod international conference on management of data, may 16-18, 20...</td>\n      <td>NaN</td>\n      <td>2000</td>\n      <td>w3qs: a query system for the world-wide webâ» in proceedings of the 21st international conferenc...</td>\n      <td>d konopnicki, o shmueli</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>53</td>\n      <td>53</td>\n      <td>conf/sigmod/2002</td>\n      <td>url:http://portal.acm.org/citation.cfm%3Fid%3D582192</td>\n      <td>proceedings of the 2002 acm sigmod international conference on management of data, madison, wisc...</td>\n      <td>NaN</td>\n      <td>2002</td>\n      <td>source international conference on management of data archive</td>\n      <td>m stonebraker</td>\n      <td>1992</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>61</td>\n      <td>61</td>\n      <td>conf/sigmod/2003</td>\n      <td>CpwYam_LYyEJ</td>\n      <td>proceedings of the 2003 acm sigmod international conference on management of data, san diego, ca...</td>\n      <td>NaN</td>\n      <td>2003</td>\n      <td>on predicting data cache behavior for real {time systems. in proceedings of the acm sigplan work...</td>\n      <td>c ferdinand, r wilhelm</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>77</td>\n      <td>77</td>\n      <td>conf/sigmod/2003</td>\n      <td>url:http://portal.acm.org/citation.cfm%3Fid%3D276304%26dl%3DACM%26coll%3Dportal%26CFID%3D1111111...</td>\n      <td>proceedings of the 2003 acm sigmod international conference on management of data, san diego, ca...</td>\n      <td>NaN</td>\n      <td>2003</td>\n      <td>source international conference on management of data archive</td>\n      <td>a tiwary, m franklin</td>\n      <td>1998</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>102</td>\n      <td>102</td>\n      <td>conf/sigmod/AbiteboulCM95</td>\n      <td>XTa-ElbpjZ8J</td>\n      <td>a database interface for file updates</td>\n      <td>s abiteboul, s cluet, t milo</td>\n      <td>1995</td>\n      <td>a logical view of structured les</td>\n      <td>s abiteboul, s cluet, t milo</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>115</td>\n      <td>115</td>\n      <td>conf/sigmod/AcharyaAFZ95</td>\n      <td>h2z51QPXcSkJ</td>\n      <td>broadcast disks: data management for asymmetric communications environments</td>\n      <td>s acharya, r alonso, m franklin, s zdonik</td>\n      <td>1995</td>\n      <td>broadca. rt l) ikr: data management for uymmetrir communications environment</td>\n      <td>s acharya, r alonso, mj franklin, s zdonik</td>\n      <td>1995</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>155</td>\n      <td>155</td>\n      <td>conf/sigmod/AdaliSS99</td>\n      <td>Cbwq3JSxrSAJ</td>\n      <td>a multimedia presentation algebra</td>\n      <td>s adali, m sapino, v subrahmanian</td>\n      <td>1999</td>\n      <td>a multimedia presentation algebra</td>\n      <td>s adah, ml sapino, vs subrahmanian</td>\n      <td>1999</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>161</td>\n      <td>161</td>\n      <td>conf/sigmod/Adelberg98</td>\n      <td>JrU1gnfH1sYJ</td>\n      <td>nodose - a tool for semi-automatically extracting semi-structured data from text documents</td>\n      <td>b adelberg</td>\n      <td>1998</td>\n      <td>nodose {a tool for semi-automatically extracting semi-structured data from text</td>\n      <td>b adelberg</td>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>167</td>\n      <td>167</td>\n      <td>conf/sigmod/AdelbergGK95</td>\n      <td>b87rz7o7R94J</td>\n      <td>applying update streams in a soft real-time database system</td>\n      <td>b adelberg, h garcia-molina, b kao</td>\n      <td>1995</td>\n      <td>rules in strip</td>\n      <td>b adelberg, h garcia-molina, b kao</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>208</td>\n      <td>208</td>\n      <td>conf/sigmod/AgichteinG03</td>\n      <td>1HwyWUB6M50J</td>\n      <td>qxtract: a building block for efficient information extraction from plain-text databases</td>\n      <td>e agichtein, l gravano</td>\n      <td>2003</td>\n      <td>querying text databases for efficient information extraction</td>\n      <td>e agichtein, l gravano</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "S.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "Metadata file is not present in the given path; proceeding to read the csv file.\n",
      "Metadata file is not present in the given path; proceeding to read the csv file.\n",
      "Reading I and J from files\n",
      "250\n",
      "250\n"
     ]
    }
   ],
   "source": [
    "i_file = \"datasets/DBLP-Scholar/I.csv\"\n",
    "j_file = \"datasets/DBLP-Scholar/J.csv\"\n",
    "\n",
    "if not os.path.isfile(i_file):\n",
    "    IJ = em.split_train_test(S, train_proportion=0.5, random_state=0)\n",
    "    I = IJ['train']\n",
    "    J = IJ['test']\n",
    "    I.to_csv(i_file, sep=\",\")\n",
    "    J.to_csv(j_file, sep=\",\")\n",
    "    print(\"Split samples into I and J\")\n",
    "else:\n",
    "    I = em.read_csv_metadata(i_file, key=\"_id\",\n",
    "                             ltable=dblp, rtable=scholar,\n",
    "                             fk_ltable=\"ltable_id\", fk_rtable='rtable_id')\n",
    "    J = em.read_csv_metadata(j_file, key=\"_id\",\n",
    "                             ltable=dblp, rtable=scholar,\n",
    "                         fk_ltable='ltable_id', fk_rtable='rtable_id')\n",
    "    print(\"Reading I and J from files\")                         \n",
    "print(len(I))\n",
    "print(len(J))"
   ]
  },
  {
   "source": [
    "## Matching(匹配)"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "em.set_key(dblp, 'id')\n",
    "em.set_key(scholar, 'id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "F = em.get_features_for_matching(dblp, scholar, validate_inferred_attr_types=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "0% [##############################] 100% | ETA: 00:00:00\n",
      "Total time elapsed: 00:00:00\n"
     ]
    }
   ],
   "source": [
    "H = em.extract_feature_vecs(I, feature_table=F, attrs_after='label', show_progress=True)"
   ]
  },
  {
   "source": [
    "## 创建学习器"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "random_state = 0 \n",
    "\n",
    "dt = em.DTMatcher(name='DecisionTree', random_state=random_state)\n",
    "rf = em.RFMatcher(name='RF', random_state=random_state)\n",
    "svm = em.SVMMatcher(name='SVM', random_state=random_state)\n",
    "ln = em.LinRegMatcher(name='LinReg')\n",
    "lg = em.LogRegMatcher(name='LogReg', random_state=random_state)\n",
    "nb = em.NBMatcher(name = 'NaiveBayes')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "/home/madao/.local/lib/python3.6/site-packages/sklearn/utils/deprecation.py:66: DeprecationWarning: Class Imputer is deprecated; Imputer was deprecated in version 0.20 and will be removed in 0.22. Import impute.SimpleImputer from sklearn instead.\n  warnings.warn(msg, category=DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "# Impute feature vectors with the mean of the column values.\n",
    "# 用列的平均值填充特征向量\n",
    "H = em.impute_table(H, exclude_attrs=['_id', 'ltable_id','rtable_id','label'], strategy='mean')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "    _id                      ltable_id     rtable_id  id_id_lev_dist  \\\n",
       "0  9171        journals/vldb/GutingS95  mGDi-Lnj27QJ            23.0   \n",
       "1  2725          conf/sigmod/ZhangRL96  46aixXBMD6sJ            20.0   \n",
       "2   634    conf/sigmod/ChakrabartiDI98  AyDb4G2EiGwJ            26.0   \n",
       "3   799  conf/sigmod/ChristophidesCS00  DqHBAYRAtyIJ            28.0   \n",
       "4  1601              conf/sigmod/Kim94  JHuJHooGoNUJ            16.0   \n",
       "\n",
       "   id_id_lev_sim  id_id_jar  id_id_jwn  id_id_exm  id_id_jac_qgm_3_qgm_3  \\\n",
       "0       0.000000   0.251208   0.251208        0.0                    0.0   \n",
       "1       0.047619   0.253968   0.253968        0.0                    0.0   \n",
       "2       0.037037   0.373457   0.373457        0.0                    0.0   \n",
       "3       0.034483   0.372605   0.372605        0.0                    0.0   \n",
       "4       0.058824   0.428105   0.428105        0.0                    0.0   \n",
       "\n",
       "   title_title_jac_qgm_3_qgm_3  ...  venue_venue_mel  venue_venue_lev_dist  \\\n",
       "0                     0.405941  ...         0.379572                  66.0   \n",
       "1                     0.681319  ...         0.593867                  33.0   \n",
       "2                     0.071429  ...         0.479163                  59.0   \n",
       "3                     0.594595  ...         0.590756                  54.0   \n",
       "4                     0.024390  ...         0.480838                  40.0   \n",
       "\n",
       "   venue_venue_lev_sim  venue_venue_nmw  venue_venue_sw  year_year_exm  \\\n",
       "0             0.043478            -59.0             1.0       0.632653   \n",
       "1             0.297872            -16.0             7.0       0.632653   \n",
       "2             0.119403            -42.0             3.0       1.000000   \n",
       "3             0.228571            -37.0            10.0       1.000000   \n",
       "4             0.090909            -23.0             2.0       0.000000   \n",
       "\n",
       "   year_year_anm  year_year_lev_dist  year_year_lev_sim  label  \n",
       "0       0.998868            2.870748           0.521542      0  \n",
       "1       0.998868            2.870748           0.521542      1  \n",
       "2       1.000000            2.000000           0.666667      0  \n",
       "3       1.000000            2.000000           0.666667      1  \n",
       "4       0.999499            3.000000           0.500000      0  \n",
       "\n",
       "[5 rows x 27 columns]"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>_id</th>\n      <th>ltable_id</th>\n      <th>rtable_id</th>\n      <th>id_id_lev_dist</th>\n      <th>id_id_lev_sim</th>\n      <th>id_id_jar</th>\n      <th>id_id_jwn</th>\n      <th>id_id_exm</th>\n      <th>id_id_jac_qgm_3_qgm_3</th>\n      <th>title_title_jac_qgm_3_qgm_3</th>\n      <th>...</th>\n      <th>venue_venue_mel</th>\n      <th>venue_venue_lev_dist</th>\n      <th>venue_venue_lev_sim</th>\n      <th>venue_venue_nmw</th>\n      <th>venue_venue_sw</th>\n      <th>year_year_exm</th>\n      <th>year_year_anm</th>\n      <th>year_year_lev_dist</th>\n      <th>year_year_lev_sim</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>9171</td>\n      <td>journals/vldb/GutingS95</td>\n      <td>mGDi-Lnj27QJ</td>\n      <td>23.0</td>\n      <td>0.000000</td>\n      <td>0.251208</td>\n      <td>0.251208</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.405941</td>\n      <td>...</td>\n      <td>0.379572</td>\n      <td>66.0</td>\n      <td>0.043478</td>\n      <td>-59.0</td>\n      <td>1.0</td>\n      <td>0.632653</td>\n      <td>0.998868</td>\n      <td>2.870748</td>\n      <td>0.521542</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2725</td>\n      <td>conf/sigmod/ZhangRL96</td>\n      <td>46aixXBMD6sJ</td>\n      <td>20.0</td>\n      <td>0.047619</td>\n      <td>0.253968</td>\n      <td>0.253968</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.681319</td>\n      <td>...</td>\n      <td>0.593867</td>\n      <td>33.0</td>\n      <td>0.297872</td>\n      <td>-16.0</td>\n      <td>7.0</td>\n      <td>0.632653</td>\n      <td>0.998868</td>\n      <td>2.870748</td>\n      <td>0.521542</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>634</td>\n      <td>conf/sigmod/ChakrabartiDI98</td>\n      <td>AyDb4G2EiGwJ</td>\n      <td>26.0</td>\n      <td>0.037037</td>\n      <td>0.373457</td>\n      <td>0.373457</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.071429</td>\n      <td>...</td>\n      <td>0.479163</td>\n      <td>59.0</td>\n      <td>0.119403</td>\n      <td>-42.0</td>\n      <td>3.0</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>2.000000</td>\n      <td>0.666667</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>799</td>\n      <td>conf/sigmod/ChristophidesCS00</td>\n      <td>DqHBAYRAtyIJ</td>\n      <td>28.0</td>\n      <td>0.034483</td>\n      <td>0.372605</td>\n      <td>0.372605</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.594595</td>\n      <td>...</td>\n      <td>0.590756</td>\n      <td>54.0</td>\n      <td>0.228571</td>\n      <td>-37.0</td>\n      <td>10.0</td>\n      <td>1.000000</td>\n      <td>1.000000</td>\n      <td>2.000000</td>\n      <td>0.666667</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1601</td>\n      <td>conf/sigmod/Kim94</td>\n      <td>JHuJHooGoNUJ</td>\n      <td>16.0</td>\n      <td>0.058824</td>\n      <td>0.428105</td>\n      <td>0.428105</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.024390</td>\n      <td>...</td>\n      <td>0.480838</td>\n      <td>40.0</td>\n      <td>0.090909</td>\n      <td>-23.0</td>\n      <td>2.0</td>\n      <td>0.000000</td>\n      <td>0.999499</td>\n      <td>3.000000</td>\n      <td>0.500000</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 27 columns</p>\n</div>"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "H.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "        Matcher  Average precision  Average recall  Average f1\n",
       "0  DecisionTree           0.863046        0.882635    0.867502\n",
       "1            RF           0.920724        0.878447    0.895470\n",
       "2           SVM           0.891378        0.672591    0.761804\n",
       "3        LinReg           0.847413        0.895039    0.865603\n",
       "4        LogReg           0.885790        0.875326    0.875706\n",
       "5    NaiveBayes           0.896039        0.866901    0.878164"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Matcher</th>\n      <th>Average precision</th>\n      <th>Average recall</th>\n      <th>Average f1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>DecisionTree</td>\n      <td>0.863046</td>\n      <td>0.882635</td>\n      <td>0.867502</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>RF</td>\n      <td>0.920724</td>\n      <td>0.878447</td>\n      <td>0.895470</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>SVM</td>\n      <td>0.891378</td>\n      <td>0.672591</td>\n      <td>0.761804</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>LinReg</td>\n      <td>0.847413</td>\n      <td>0.895039</td>\n      <td>0.865603</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>LogReg</td>\n      <td>0.885790</td>\n      <td>0.875326</td>\n      <td>0.875706</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>NaiveBayes</td>\n      <td>0.896039</td>\n      <td>0.866901</td>\n      <td>0.878164</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "result = em.select_matcher([dt, rf, svm, ln, lg, nb],       table=H, exclude_attrs=['_id', 'ltable_id',             'rtable_id', 'label'], k=5,target_attr='label',\n",
    "    metric_to_select_matcher='f1',\n",
    "    random_state=0)\n",
    "result['cv_stats']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "           Name  \\\n",
       "0  DecisionTree   \n",
       "1            RF   \n",
       "2           SVM   \n",
       "3        LinReg   \n",
       "4        LogReg   \n",
       "5    NaiveBayes   \n",
       "\n",
       "                                                                            Matcher  \\\n",
       "0          <py_entitymatching.matcher.dtmatcher.DTMatcher object at 0x7f55fb9a6a58>   \n",
       "1          <py_entitymatching.matcher.rfmatcher.RFMatcher object at 0x7f55fb9a6400>   \n",
       "2        <py_entitymatching.matcher.svmmatcher.SVMMatcher object at 0x7f55fb9a6e80>   \n",
       "3  <py_entitymatching.matcher.linregmatcher.LinRegMatcher object at 0x7f55fb9a6630>   \n",
       "4  <py_entitymatching.matcher.logregmatcher.LogRegMatcher object at 0x7f55fb9a6550>   \n",
       "5          <py_entitymatching.matcher.nbmatcher.NBMatcher object at 0x7f55fb9a60b8>   \n",
       "\n",
       "   Num folds    Fold 1    Fold 2    Fold 3    Fold 4    Fold 5  Mean score  \n",
       "0          5  0.947368  0.806452  0.791667  0.838710  0.931034    0.863046  \n",
       "1          5  1.000000  0.888889  0.818182  0.896552  1.000000    0.920724  \n",
       "2          5  0.857143  0.863636  0.777778  0.958333  1.000000    0.891378  \n",
       "3          5  0.894737  0.833333  0.730769  0.875000  0.903226    0.847413  \n",
       "4          5  0.947368  0.827586  0.760000  0.931034  0.962963    0.885790  \n",
       "5          5  0.950000  0.862069  0.782609  0.920000  0.965517    0.896039  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Name</th>\n      <th>Matcher</th>\n      <th>Num folds</th>\n      <th>Fold 1</th>\n      <th>Fold 2</th>\n      <th>Fold 3</th>\n      <th>Fold 4</th>\n      <th>Fold 5</th>\n      <th>Mean score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>DecisionTree</td>\n      <td>&lt;py_entitymatching.matcher.dtmatcher.DTMatcher object at 0x7f55fb9a6a58&gt;</td>\n      <td>5</td>\n      <td>0.947368</td>\n      <td>0.806452</td>\n      <td>0.791667</td>\n      <td>0.838710</td>\n      <td>0.931034</td>\n      <td>0.863046</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>RF</td>\n      <td>&lt;py_entitymatching.matcher.rfmatcher.RFMatcher object at 0x7f55fb9a6400&gt;</td>\n      <td>5</td>\n      <td>1.000000</td>\n      <td>0.888889</td>\n      <td>0.818182</td>\n      <td>0.896552</td>\n      <td>1.000000</td>\n      <td>0.920724</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>SVM</td>\n      <td>&lt;py_entitymatching.matcher.svmmatcher.SVMMatcher object at 0x7f55fb9a6e80&gt;</td>\n      <td>5</td>\n      <td>0.857143</td>\n      <td>0.863636</td>\n      <td>0.777778</td>\n      <td>0.958333</td>\n      <td>1.000000</td>\n      <td>0.891378</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>LinReg</td>\n      <td>&lt;py_entitymatching.matcher.linregmatcher.LinRegMatcher object at 0x7f55fb9a6630&gt;</td>\n      <td>5</td>\n      <td>0.894737</td>\n      <td>0.833333</td>\n      <td>0.730769</td>\n      <td>0.875000</td>\n      <td>0.903226</td>\n      <td>0.847413</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>LogReg</td>\n      <td>&lt;py_entitymatching.matcher.logregmatcher.LogRegMatcher object at 0x7f55fb9a6550&gt;</td>\n      <td>5</td>\n      <td>0.947368</td>\n      <td>0.827586</td>\n      <td>0.760000</td>\n      <td>0.931034</td>\n      <td>0.962963</td>\n      <td>0.885790</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>NaiveBayes</td>\n      <td>&lt;py_entitymatching.matcher.nbmatcher.NBMatcher object at 0x7f55fb9a60b8&gt;</td>\n      <td>5</td>\n      <td>0.950000</td>\n      <td>0.862069</td>\n      <td>0.782609</td>\n      <td>0.920000</td>\n      <td>0.965517</td>\n      <td>0.896039</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "result['drill_down_cv_stats']['precision']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "           Name  \\\n",
       "0  DecisionTree   \n",
       "1            RF   \n",
       "2           SVM   \n",
       "3        LinReg   \n",
       "4        LogReg   \n",
       "5    NaiveBayes   \n",
       "\n",
       "                                                                            Matcher  \\\n",
       "0          <py_entitymatching.matcher.dtmatcher.DTMatcher object at 0x7f55fb9a6a58>   \n",
       "1          <py_entitymatching.matcher.rfmatcher.RFMatcher object at 0x7f55fb9a6400>   \n",
       "2        <py_entitymatching.matcher.svmmatcher.SVMMatcher object at 0x7f55fb9a6e80>   \n",
       "3  <py_entitymatching.matcher.linregmatcher.LinRegMatcher object at 0x7f55fb9a6630>   \n",
       "4  <py_entitymatching.matcher.logregmatcher.LogRegMatcher object at 0x7f55fb9a6550>   \n",
       "5          <py_entitymatching.matcher.nbmatcher.NBMatcher object at 0x7f55fb9a60b8>   \n",
       "\n",
       "   Num folds    Fold 1  Fold 2    Fold 3    Fold 4    Fold 5  Mean score  \n",
       "0          5  0.750000    1.00  0.863636  0.928571  0.870968    0.882635  \n",
       "1          5  0.750000    0.96  0.818182  0.928571  0.935484    0.878447  \n",
       "2          5  0.500000    0.76  0.636364  0.821429  0.645161    0.672591  \n",
       "3          5  0.708333    1.00  0.863636  1.000000  0.903226    0.895039  \n",
       "4          5  0.750000    0.96  0.863636  0.964286  0.838710    0.875326  \n",
       "5          5  0.791667    1.00  0.818182  0.821429  0.903226    0.866901  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Name</th>\n      <th>Matcher</th>\n      <th>Num folds</th>\n      <th>Fold 1</th>\n      <th>Fold 2</th>\n      <th>Fold 3</th>\n      <th>Fold 4</th>\n      <th>Fold 5</th>\n      <th>Mean score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>DecisionTree</td>\n      <td>&lt;py_entitymatching.matcher.dtmatcher.DTMatcher object at 0x7f55fb9a6a58&gt;</td>\n      <td>5</td>\n      <td>0.750000</td>\n      <td>1.00</td>\n      <td>0.863636</td>\n      <td>0.928571</td>\n      <td>0.870968</td>\n      <td>0.882635</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>RF</td>\n      <td>&lt;py_entitymatching.matcher.rfmatcher.RFMatcher object at 0x7f55fb9a6400&gt;</td>\n      <td>5</td>\n      <td>0.750000</td>\n      <td>0.96</td>\n      <td>0.818182</td>\n      <td>0.928571</td>\n      <td>0.935484</td>\n      <td>0.878447</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>SVM</td>\n      <td>&lt;py_entitymatching.matcher.svmmatcher.SVMMatcher object at 0x7f55fb9a6e80&gt;</td>\n      <td>5</td>\n      <td>0.500000</td>\n      <td>0.76</td>\n      <td>0.636364</td>\n      <td>0.821429</td>\n      <td>0.645161</td>\n      <td>0.672591</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>LinReg</td>\n      <td>&lt;py_entitymatching.matcher.linregmatcher.LinRegMatcher object at 0x7f55fb9a6630&gt;</td>\n      <td>5</td>\n      <td>0.708333</td>\n      <td>1.00</td>\n      <td>0.863636</td>\n      <td>1.000000</td>\n      <td>0.903226</td>\n      <td>0.895039</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>LogReg</td>\n      <td>&lt;py_entitymatching.matcher.logregmatcher.LogRegMatcher object at 0x7f55fb9a6550&gt;</td>\n      <td>5</td>\n      <td>0.750000</td>\n      <td>0.96</td>\n      <td>0.863636</td>\n      <td>0.964286</td>\n      <td>0.838710</td>\n      <td>0.875326</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>NaiveBayes</td>\n      <td>&lt;py_entitymatching.matcher.nbmatcher.NBMatcher object at 0x7f55fb9a60b8&gt;</td>\n      <td>5</td>\n      <td>0.791667</td>\n      <td>1.00</td>\n      <td>0.818182</td>\n      <td>0.821429</td>\n      <td>0.903226</td>\n      <td>0.866901</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "result['drill_down_cv_stats']['recall']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "           Name  \\\n",
       "0  DecisionTree   \n",
       "1            RF   \n",
       "2           SVM   \n",
       "3        LinReg   \n",
       "4        LogReg   \n",
       "5    NaiveBayes   \n",
       "\n",
       "                                                                            Matcher  \\\n",
       "0          <py_entitymatching.matcher.dtmatcher.DTMatcher object at 0x7f55fb9a6a58>   \n",
       "1          <py_entitymatching.matcher.rfmatcher.RFMatcher object at 0x7f55fb9a6400>   \n",
       "2        <py_entitymatching.matcher.svmmatcher.SVMMatcher object at 0x7f55fb9a6e80>   \n",
       "3  <py_entitymatching.matcher.linregmatcher.LinRegMatcher object at 0x7f55fb9a6630>   \n",
       "4  <py_entitymatching.matcher.logregmatcher.LogRegMatcher object at 0x7f55fb9a6550>   \n",
       "5          <py_entitymatching.matcher.nbmatcher.NBMatcher object at 0x7f55fb9a60b8>   \n",
       "\n",
       "   Num folds    Fold 1    Fold 2    Fold 3    Fold 4    Fold 5  Mean score  \n",
       "0          5  0.837209  0.892857  0.826087  0.881356  0.900000    0.867502  \n",
       "1          5  0.857143  0.923077  0.818182  0.912281  0.966667    0.895470  \n",
       "2          5  0.631579  0.808511  0.700000  0.884615  0.784314    0.761804  \n",
       "3          5  0.790698  0.909091  0.791667  0.933333  0.903226    0.865603  \n",
       "4          5  0.837209  0.888889  0.808511  0.947368  0.896552    0.875706  \n",
       "5          5  0.863636  0.925926  0.800000  0.867925  0.933333    0.878164  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Name</th>\n      <th>Matcher</th>\n      <th>Num folds</th>\n      <th>Fold 1</th>\n      <th>Fold 2</th>\n      <th>Fold 3</th>\n      <th>Fold 4</th>\n      <th>Fold 5</th>\n      <th>Mean score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>DecisionTree</td>\n      <td>&lt;py_entitymatching.matcher.dtmatcher.DTMatcher object at 0x7f55fb9a6a58&gt;</td>\n      <td>5</td>\n      <td>0.837209</td>\n      <td>0.892857</td>\n      <td>0.826087</td>\n      <td>0.881356</td>\n      <td>0.900000</td>\n      <td>0.867502</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>RF</td>\n      <td>&lt;py_entitymatching.matcher.rfmatcher.RFMatcher object at 0x7f55fb9a6400&gt;</td>\n      <td>5</td>\n      <td>0.857143</td>\n      <td>0.923077</td>\n      <td>0.818182</td>\n      <td>0.912281</td>\n      <td>0.966667</td>\n      <td>0.895470</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>SVM</td>\n      <td>&lt;py_entitymatching.matcher.svmmatcher.SVMMatcher object at 0x7f55fb9a6e80&gt;</td>\n      <td>5</td>\n      <td>0.631579</td>\n      <td>0.808511</td>\n      <td>0.700000</td>\n      <td>0.884615</td>\n      <td>0.784314</td>\n      <td>0.761804</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>LinReg</td>\n      <td>&lt;py_entitymatching.matcher.linregmatcher.LinRegMatcher object at 0x7f55fb9a6630&gt;</td>\n      <td>5</td>\n      <td>0.790698</td>\n      <td>0.909091</td>\n      <td>0.791667</td>\n      <td>0.933333</td>\n      <td>0.903226</td>\n      <td>0.865603</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>LogReg</td>\n      <td>&lt;py_entitymatching.matcher.logregmatcher.LogRegMatcher object at 0x7f55fb9a6550&gt;</td>\n      <td>5</td>\n      <td>0.837209</td>\n      <td>0.888889</td>\n      <td>0.808511</td>\n      <td>0.947368</td>\n      <td>0.896552</td>\n      <td>0.875706</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>NaiveBayes</td>\n      <td>&lt;py_entitymatching.matcher.nbmatcher.NBMatcher object at 0x7f55fb9a60b8&gt;</td>\n      <td>5</td>\n      <td>0.863636</td>\n      <td>0.925926</td>\n      <td>0.800000</td>\n      <td>0.867925</td>\n      <td>0.933333</td>\n      <td>0.878164</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "result['drill_down_cv_stats']['f1']"
   ]
  },
  {
   "source": [
    "### Train classfiers on I and test on J(利用I样本训练和利用J样本训练)"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "classifiers = np.array([dt, rf, svm, ln, lg, nb])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "0% [##############################] 100% | ETA: 00:00:00\n",
      "Total time elapsed: 00:00:00\n"
     ]
    }
   ],
   "source": [
    "# Convert J into a set of feature vectors using F\n",
    "# 利用F将J转换为特征向量\n",
    "L = em.extract_feature_vecs(J, feature_table=F, attrs_after='label', show_progress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "/home/madao/.local/lib/python3.6/site-packages/sklearn/utils/deprecation.py:66: DeprecationWarning: Class Imputer is deprecated; Imputer was deprecated in version 0.20 and will be removed in 0.22. Import impute.SimpleImputer from sklearn instead.\n  warnings.warn(msg, category=DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "# Impute feature vectors with the mean of the column values\n",
    "# 利用每一栏的平均值填充特征向量\n",
    "L = em.impute_table(L, exclude_attrs=['_id', 'ltable_id', 'rtable_id', 'label'], strategy='mean')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "DecisionTree\nPrecision : 86.36% (114/132)\nRecall : 84.44% (114/135)\nF1 : 85.39%\nFalse positives : 18 (out of 132 positive predictions)\nFalse negatives : 21 (out of 118 negative predictions)\n\nRF\nPrecision : 93.7% (119/127)\nRecall : 88.15% (119/135)\nF1 : 90.84%\nFalse positives : 8 (out of 127 positive predictions)\nFalse negatives : 16 (out of 123 negative predictions)\n\nSVM\nPrecision : 94.0% (94/100)\nRecall : 69.63% (94/135)\nF1 : 80.0%\nFalse positives : 6 (out of 100 positive predictions)\nFalse negatives : 41 (out of 150 negative predictions)\n\nLinReg\nPrecision : 86.21% (125/145)\nRecall : 92.59% (125/135)\nF1 : 89.29%\nFalse positives : 20 (out of 145 positive predictions)\nFalse negatives : 10 (out of 105 negative predictions)\n\nLogReg\nPrecision : 87.86% (123/140)\nRecall : 91.11% (123/135)\nF1 : 89.45%\nFalse positives : 17 (out of 140 positive predictions)\nFalse negatives : 12 (out of 110 negative predictions)\n\nNaiveBayes\nPrecision : 87.86% (123/140)\nRecall : 91.11% (123/135)\nF1 : 89.45%\nFalse positives : 17 (out of 140 positive predictions)\nFalse negatives : 12 (out of 110 negative predictions)\n\n"
     ]
    }
   ],
   "source": [
    "for c in classifiers:\n",
    "    # Train using feature vectors from I\n",
    "    # 利用来自I的特征向量训练\n",
    "    c.fit(table=H, exclude_attrs=['_id', 'ltable_id', 'rtable_id', 'label'], target_attr='label')\n",
    "\n",
    "    # Predict on L(在L上预测)\n",
    "    predictions = c.predict(table=L, exclude_attrs=['_id', 'ltable_id', 'rtable_id', 'label'], append=True, target_attr='predicted', inplace=False)\n",
    "\n",
    "    predictions[['_id', 'ltable_id', 'rtable_id', 'predicted', 'label']].head()\n",
    "\n",
    "    print(c.name)\n",
    "    eval_result = em.eval_matches(predictions, 'label', 'predicted')\n",
    "    em.print_eval_summary(eval_result)\n",
    "    print()"
   ]
  }
 ]
}